#include <string.h>
#include <stdio.h>

void itoa_linux( int i,char * string) 
{
	int power, j;

	j = i; 

	for (power = 1; j >= 10; j /= 10) {	// 个十百千万...
		power *= 10; 
		printf("line = %d\tpower = %d\n", __LINE__, power);
	}

	for (; power > 0; power /= 10)	// 从高位开始
	{
		printf("line = %d\tpower = %d\n", __LINE__, power);
		*string++ = '0' + i / power;	// 转换成字符
		i %= power; 
		printf("line = %d\ti = %d\n", __LINE__, i);
	}

	*string = '\0';
} 

char * itoa_solaris(long n, int base) 
{
	register char *p; 
	register int minus; 
	static char buf[36];

	p = &buf[36]; 
	*--p = '\0'; 

	if (n < 0) { 
		minus = 1; n = -n; 
	} 
	else 
		minus = 0; 

	if (n == 0)  
		*--p = '0'; 
	else 
		while (n > 0) {
			*--p = "0123456789abcdef"[n % base]; 
			n /= base; 
		} 


	if (minus) 
		*--p = '-';

	return p;
}

int main()
{
	int i = 100;
	char string[20];
	char *p;

	memset(string, 0, 20);
	itoa_linux(i, string);
	printf("string = \"%s\"\n", string);

	i = 1000;
	p = itoa_solaris(i, 10);
	printf("string = \"%s\"\n", p);

	i = 1000;
	p = itoa_solaris(i, 16);
	printf("string = \"%s\"\n", p);

	i = 1000;
	p = itoa_solaris(i, 8);
	printf("string = \"%s\"\n", p);


	return 0;
}
